-- UICoffin
-- Created by chengb Nov/12/2015
-- 圣棺界面

UICoffin = class("UICoffin", function()
    return cc.Layer:create();
end);

function UICoffin.create(gridData)
    return UICoffin.new(gridData);
end

-- 构造函数
function UICoffin:ctor(gridData)
    self:setName("UICoffin");
    local node = cc.CSLoader:createNode("layout/dungeon/special/Coffin.csb");
    self:addChild(node);
    self.node = node;

    self:setLocalZOrder(UIMgr.TOP_MENU_ZORDER + 1);

    self.gridData = gridData;

    -- 注册事件处理回调函数
    self:registerEventCallback();

    -- 注册点击事件
    self:registerTouchEvent();

    -- 重绘
    self:redraw();

    -- 适配
    self:resize();
end

-- 适配
function UICoffin:resize()
    local node = self.node;

    -- bg居中显示并充满整个屏幕
    AlignM.fitToScreen(node, "bg");

    -- CT居中并缩放至合适大小
    AlignM.alignToCenter(node, "CT");

    -- BT置于底部并缩放至合适大小
    AlignM.alignToBottomCenter(node, "BT");
end

-- 注册事件处理回调函数
function UICoffin:registerEventCallback()
    self:registerScriptHandler(function(ev)
        if ev == "exit" then
            cclog("UICoffin 界面析构清理");
            EventMgr.removeAll("UICoffin");

            if UIDungeonMgr:getCurLevel() then
                UIDungeonMgr:getCurLevel():showMenu();
            end
        elseif eventType == "enter" then
            if UIDungeonMgr:getCurLevel() then
                UIDungeonMgr:getCurLevel():hideMenu();
            end
        end
    end);
end

-- 注册点击事件
function UICoffin:registerTouchEvent()
  -- 注册返回按钮点击事件
    local node = self.node;
    local btnBack = findChildByName(node, "BT/btn_back");
    local function onBackClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            AudioM.playFx("button_return");
            -- 关闭当前界面
            UIMgr.getCurrentScene():removeFormByName("UICoffin");
        end
    end
    AddBackEventHandler(self, btnBack, onBackClick);
    TextStyleM.setTextStyle(btnBack, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    btnBack:setTitleText(getLocStr("btn_text_back"));

    -- 注册选择按钮点击事件
    local btnOk  = findChildByName(self.node, "CT/btn_ok");
    local function onOkClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            -- 领取奖励
            if DungeonActionM.go("take_coffin_bonus", self.gridData:getPos(), self.selectIndex) then
                closeFormByName("UICoffin");
            end
        end
    end
    btnOk:addTouchEventListener(onOkClick);
    TextStyleM.setTextStyle(btnOk, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    btnOk:setTitleText(getLocStr("btn_text_ok"));
end

-- 重绘
function UICoffin:redraw()
    -- 标题
    local titleLabel = findChildByName(self.node, "CT/title_node/title");
    titleLabel:setString(getLocStr("coffin"));
    TextStyleM.setArtWordTitleStyle(titleLabel);

    local kerning, kerning2 = 0, 0;
    if getLang() ~= "zh" and getLang() ~= "tw" then
        kerning, kerning2 = -3, -1;
    end

    -- 描述
    local descLabel  = findChildByName(self.node, "CT/desc");
    TextStyleM.setTextStyle(descLabel, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_DARK_BROWN, false, kerning2);
    applyString(descLabel, getLocStr("coffin_desc"));

    -- 图标
    local iconImg = findChildByName(self.node, "CT/icon");
    iconImg:loadTexture(getLevelItemIconPath("coffin"));
    iconImg:setScale(1.0);

    -- 绘制奖励
    local bonusList = self.gridData.bonus;
    self:initBonusItem(bonusList);
end

-- 绘制奖励
function UICoffin:initBonusItem(bonusList)
    local parentNode = findChildByName(self.node, "CT/bonus");

    -- 先清空
    parentNode:removeAllChildren();

    local itemNode = cc.CSLoader:createNode("layout/workshop/StoreItem.csb");
    itemNode = replaceNodeWithWidget(itemNode);

    local itemWidth = 128;
    local gap = 40;
    local widget = ccui.Widget:create();
    local totalWidth = #bonusList * (itemWidth + gap);
    local totalCount = #bonusList;
    local msg = "";

    self.allLightImgs = {};

    for i = 1, totalCount do
        local bonus = bonusList[i];

        local child = itemNode:clone();
        child:setPositionX((i - 1) * (itemWidth + gap));

        -- 图标
        local iconImg = findChildByName(child, "icon");
        if bonus[1] == 1 then
            -- 物品
            local iconPath = getItemIconPath(ItemM.query(bonus[2], "icon"));
            iconImg:loadTexture(iconPath)
        elseif bonus[1] == 2 then
            -- 属性
            local iconPath = getLargeAttribIconPath(FieldsM.query(bonus[2], "icon"));
            iconImg:loadTexture(iconPath);
        end

        -- 个数
        local numLabel = findChildByName(child, "num");
        TextStyleM.setTextStyle(numLabel, TextStyleM.TEXT_SIZE_SMALL2, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true, -2);
        numLabel:setString(tostring(bonus[3]));

        -- 选中光效
        local lightImg = findChildByName(child, "focus");
        lightImg:setTag(i);
        table.insert(self.allLightImgs, lightImg);

        -- 注册方框点击事件
        local frame = findChildByName(child, "bg");
        iconImg:setTouchEnabled(false);
        frame:setTouchEnabled(true);
        frame:setTag(i);
        local function onFrameClick(sender, eventType)
            if eventType == ccui.TouchEventType.ended then
                local index = sender:getTag();
                if self.selectIndex == index then
                    return;
                end

                -- 选中该奖励
                self:setBonusSelected(index);
            end
        end
        frame:addTouchEventListener(onFrameClick);

        widget:addChild(child);
    end

    widget:setPositionX(-totalWidth / 2 + (itemWidth + gap) / 2);
    parentNode:addChild(widget);

    -- 默认选中第一个奖励
    self:setBonusSelected(1);
end

-- 设置选中奖励效果
function UICoffin:setBonusSelected(index)
    self.selectIndex = index;

    -- 设置选中效果
    for _, lightImg in pairs(self.allLightImgs) do
        if lightImg:getTag() == self.selectIndex then
            lightImg:setVisible(true);
        else
            lightImg:setVisible(false);
        end
    end

    local bonus = self.gridData.bonus[self.selectIndex];
    local amount = bonus[3];
    local name = "";
    if bonus[1] == 1 then
        name = ItemM.query(bonus[2], "name");
    elseif bonus[1] == 2 then
        name = FieldsM.getFieldName(bonus[2]);
    end

    local chooseLabel = findChildByName(self.node, "CT/choose_tip");
    local tip = string.format(getLocStr("bonus_choise_tip"), name, amount);
    TextStyleM.setTextStyle(chooseLabel, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_DARK_BROWN, false, kerning2);
    applyString(chooseLabel, tip);

    -- TODO 这里有一个坑：需要重新设置一下描述，否则applyString后文本会变白
    -- 描述
    local descLabel  = findChildByName(self.node, "CT/desc");
    TextStyleM.setTextStyle(descLabel, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_DARK_BROWN, false, kerning2);
    applyString(descLabel, getLocStr("coffin_desc"));
end



